package com.ftwj.service.impl;

import cn.hutool.core.date.DateTime;
import com.ftwj.entity.SysUserRole;
import com.ftwj.mapper.SysUserRoleMapper;
import com.ftwj.service.SysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysUserRoleServiceImpl implements SysUserRoleService {
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;
    @Override
    public void assignRolesToUser(String userId, List<String> roleIds) {
        // 首先删除用户的所有角色关联
        sysUserRoleMapper.deleteById(userId);

        // 然后根据新的角色ID列表插入新的用户角色关联
        for (String roleId : roleIds) {
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(userId);
            sysUserRole.setRoleId(roleId);
            sysUserRole.setCreateTime(DateTime.now());
            sysUserRole.setUpdateTime(DateTime.now());
            sysUserRoleMapper.insert(sysUserRole);
        }
    }
}


